#!/bin/sh
cd "${APP_DIR}"
if [ ${#DISTDIR} -lt 1 ] ; then
    echo "ERROR from $0:" >&2
    echo "Cannot compile - DISTDIR is missing!" >&2
    [ "a${DO_READ_WAIT}" = "aok" ] && read WAIT
    exit 1
fi

if [ ${#SRCDIR} -lt 1 ] || [ ! -d "${SRCDIR}" ] ; then
    echo "ERROR from $0:" >&2
    echo "Cannot compile - SRCDIR is missing!" >&2
    [ "a${DO_READ_WAIT}" = "aok" ] && read WAIT
    exit 1
fi
if [ ${#BUILDDIR} -lt 1 ] ; then
    echo "ERROR from $0:" >&2
    echo "Cannot compile - BUILDDIR is missing!" >&2
    [ "a${DO_READ_WAIT}" = "aok" ] && read WAIT
    exit 1
fi

if [ ! -d "${BUILDDIR}" ]  ; then
    if ! mkdir "${BUILDDIR}"  ; then
	echo "Cannot create BUILDDIR" >&2
	exit 1
    fi
fi

if [ ! -d "${DISTDIR}" ]  ; then
    if ! mkdir "${DISTDIR}"  ; then
	echo "Cannot create DISTDIR" >&2
	exit 1
    fi
fi

echo "Compiling ${APP_DIR} - please wait..." >&2 

cd "${BUILDDIR}" && cmake  -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="${DISTDIR}" "${SRCDIR}" && make && make install && exit 0
echo Compile failed >&2 
echo Press Return... >&2 
[ "a${DO_READ_WAIT}" = "aok" ] && read WAIT 
exit 1 
